<?php
  /**
   * This class provides functions to format and display errors
   *
   * @author Macdonald Terrence Robinson
   * @package Core
   *
   */
  final class ErrorHandler
  {
    public static function getTraceInfo($showVerbose)
    {
      $backTrace = debug_backtrace();

      $newBacktrace = array();

      foreach ($backTrace as $key=>$value)
      {
        if( !isset($value['file']))
          continue;

        $errorFile =  self::changeSlashes($value['file']);

        if(( strpos($errorFile, Config::$siteDir.'index.php') === false ) && ( strpos($errorFile, Config::$coreDir) === false ))
          $newBacktrace[] = $value;
      }

      $newBacktrace = array_reverse($newBacktrace);
      
      if($showVerbose)
        $newBacktrace = $backTrace;
        
      $errorMessage = '<br />';
      
      foreach ($newBacktrace as $key=>$value)
      {
        if( isset($value['file']) )
          $errorMessage .= '<br />in file ( <strong>'.$value['file'].'</strong> ) at line ( <strong>'.$value['line'].'</strong> ) while calling function ( <strong>'.$value['function'].'</strong> ) <br />';
      }
      
      return $errorMessage;
      
    }
    
    public static function runTimeError($errorMessage, $showVerbose = false)
    {
      $errorMessage = '<br />'.$errorMessage.self::getTraceInfo($showVerbose);
      echo $errorMessage;
    }
    
    public static function changeSlashes($filePath)
    {
      $filePath = str_replace('\\', Config::$directorySeperator, $filePath );
      return $filePath;
    }

  }
?>
